// 创建一个表单，并返回id
Cypress.Commands.add('addFormToProject', function (projectId, formName) {
  cy.request('POST', `/api/v1/projects/${projectId}/forms`, {
    name: formName,
    setting: {
      delete_fieldds: [],
      fields: [],
      join_table: [],
      sort: []
    }
  }).then(res => {
    expect(res.body.code).to.be.eq(1);
    expect(res.body.data.name).to.be.eq(formName);
    cy.wrap(res.body.data.id).as('formId');
  });
  return cy.get('@formId');
});

// 根据id删除表单
Cypress.Commands.add('deleteFormFromProject', function (projectId, formId) {
  cy.request('DELETE', `/api/v1/projects/${projectId}/forms/${formId}`).then(res => {
    expect(res.body.code).to.be.eq(1);
    expect(res.body.data.id).to.be.eq(formId);
  });
});

// 获取指定名称的表单
Cypress.Commands.add('getFormFromList', function (formName) {
  cy.get('.operation-right[data-cy=ProjectTask-Info-index-div-20]').click();
  cy.get('.name[data-cy=ProjectTask-Info-index-span-101]').click();
  cy.contains('.form-list-box[data-cy=ProjectTask-Info-index-div-151]', formName);
});

// 打开指定名称的表单配置页面
Cypress.Commands.add('openFormConfigurePage', function (formName) {
  cy.getFormFromList(formName).contains('.running[data-cy=ProjectTask-Info-index-span]', '配置字段').click();
});

// 创建指定名称的表单项
Cypress.Commands.add('formItemPush', function (itemName, config) {
  cy.contains('[data-cy=components-FormDesigner2-container-div-14-header]', '表单设计').click();
  cy.contains('[data-rbd-draggable-id]', itemName)
    .focus()
    .trigger('keydown', { keyCode: 32 })
    .trigger('keydown', { keyCode: 39, force: true })
    .trigger('keydown', { keyCode: 32, force: true })
    .wait(250);
  for (const [fieldName, htmlType, value] of config) {
    cy.contains('[data-cy=common-formItem-index-div-baseItem]', fieldName).then($el => {
      cy.wrap($el).as('valueBox');
      cy.get('@valueBox').find(htmlType).clear().type(value);
    });
  }
});

// 删除指定名称的表单项
Cypress.Commands.add('deleteFormItem', function (itemName) {
  cy.contains('[data-cy=components-FormDesigner2-container-div-14-header]', '表单设计').click();
  cy.contains('[class^=design-content_design-content-item][data-cy=components-FormDesigner2-design-content-div]', itemName)
    .as('displayBox');
  cy.get('@displayBox')
    .realHover()
    .find('[class^=design-content_operateBtn][data-cy=components-FormDesigner2-design-content-div]:last-child')
    .click()
});

